package com.rem.rabbitmq.boot.CbackupExchange;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * @author Rem
 * @date 2022/1/8
 */

@Slf4j
@RestController
@RequestMapping("/backup")
@Api(tags = "备份交换机")
public class BackupExchangeController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @ApiOperation(value = "备份交换机发送一条确认的消息")
    @GetMapping("/sendMsg/{message}")
    public void sendMsg(@PathVariable String message) {
        log.info("当前时间：{}，发送消息开始:{}", LocalDateTime.now(), message);

        CorrelationData correlationData = new CorrelationData("111");
        rabbitTemplate.convertAndSend(BackupExchangeConfig.NORMAL_EXCHANGE, BackupExchangeConfig.NORMAL_ROUTING_KEY, BackupExchangeConfig.NORMAL_ROUTING_KEY + ":" + message, correlationData);

        /**
         * 发送到一个假的exchange上
         * 消息ack为false
         */
        CorrelationData correlationData2 = new CorrelationData("222");

        rabbitTemplate.convertAndSend("FAKE_EXCHANGE", BackupExchangeConfig.NORMAL_EXCHANGE, BackupExchangeConfig.NORMAL_ROUTING_KEY + ":" + message, correlationData2);

        /**
         * 发送到一个假的routingKey上
         * 设置了备份交换机 消息不会被退回 （备份交换机的级别比设置退回消息权限高）
         */
        CorrelationData correlationData3 = new CorrelationData("333");
        rabbitTemplate.convertAndSend(BackupExchangeConfig.NORMAL_EXCHANGE, "FAKE_ROUTING_KEY", "FAKE_ROUTING_KEY" + ":" + message, correlationData3);


        log.info("当前时间：{}，发送消息完毕:{}", LocalDateTime.now(), message);
    }


}
